package com.example.system.controller;

import com.example.core.constants.HttpConstants;
import com.example.core.controller.BaseController;
import com.example.core.domain.R;
import com.example.core.domain.VO.LoginUserVO;
import com.example.core.utils.StringUtils;
import com.example.scurity.service.TokenService;
import com.example.scurity.utils.SecurityUtils;
import com.example.system.domain.sysUser.dto.SysUserDTO;
import com.example.system.domain.sysUser.dto.SysUserSaveDto;
import com.example.system.service.impl.SysUserServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/sysUser")
@Tag(name="管理员用户API")
public class SysUserController extends BaseController {

    @Autowired
    private TokenService tokenService;

    @Resource
    private SysUserServiceImpl sysUserService;
    @Operation(summary = "管理员登录接口",description = "通过用户名获取数据库中的密码比对是否与输入密码一致")
    @ApiResponse(responseCode = "1000",description = "操作成功")
    @ApiResponse(responseCode = "2000",description = "服务器繁忙请稍后重试")
    @PostMapping("/login")
    public R<String> login(@RequestBody SysUserDTO sysUserDto) {

        return  R.ok(sysUserService.login(sysUserDto));
    }

    @DeleteMapping("/logout")
    public R<?> logout(HttpServletRequest request) {
        String token = SecurityUtils.getToken(request);
        if (StringUtils.isNotEmpty(token)) {
            tokenService.delLoginUser(token);
        }
        return R.ok();
    }




    @GetMapping("/info")
    public R<LoginUserVO> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token){
        return  sysUserService.info(token);
    }




    @PostMapping("/add")
    @Operation(summary = "新增管理员",description = "根据提供的信息新增管理员用户")
    @ApiResponse(responseCode = "1000",description = "操作成功")
    @ApiResponse(responseCode = "2000",description = "服务器繁忙请稍后重试")
    @ApiResponse(responseCode = "3101",description = "用户已经存在")
    public R<Void> add(@RequestBody @Validated SysUserSaveDto sysUserSaveDto) {
       return toResult(sysUserService.add(sysUserSaveDto));
    }
}
